package cn.doitedu.rtmk.data_moni;

import org.apache.commons.lang3.RandomUtils;
import org.roaringbitmap.RoaringBitmap;
import redis.clients.jedis.Jedis;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

/**
 * @Author: deep as the sea
 * @Site: <a href="www.51doit.com">多易教育</a>
 * @QQ: 657270652
 * @Date: 2023/4/14
 * @Desc: 学大数据，到多易教育
 * 规则元信息  数据模拟器
 **/
public class RuleMetaDataMoniV3 {

    public static void main(String[] args) throws SQLException, IOException {

        //String paramJson1 = "{\"rule_id\":1,\"rule_model_id\":1,\"triggerEventId\":\"ad_click\",\"triggerEventPropertyName\":\"ad_id\",\"triggerEventPropertyValue\":\"ad001\"}";
        //insertRule(new int[]{1,2,3,5,7},1,1,paramJson1);


        // String paramJson2 = "{\"rule_id\":2,\"rule_model_id\":1,\"triggerEventId\":\"page_load\",\"triggerEventPropertyName\":\"url\",\"triggerEventPropertyValue\":\"/page/001\"}";
        // insertRule(new int[]{1,2,3},2,1,paramJson2);


//        String paramJson3 = "{\n" +
//                "  \"rule_id\":3,\n" +
//                "  \"rule_model_id\":2,\n" +
//                "  \"static_profile_conditions\":[\n" +
//                "    {\"tag_name\":\"TAG0101\",\"compare\":\">\",\"tag_value\":[200]},\n" +
//                "    {\"tag_name\":\"TAG0102\",\"compare\":\"bt\",\"tag_value\":[20,30]},\n" +
//                "    {\"tag_name\":\"TAG0103\",\"compare\":\"=\",\"tag_value\":[\"male\"]}\n" +
//                "  ],\n" +
//                "  \"dynamic_profile_conditions\":{\n" +
//                "    \"event_id\":\"add_cart\",\n" +
//                "    \"event_property_name\":\"item_id\",\n" +
//                "    \"event_property_value\":\"item001\",\n" +
//                "    \"minCount\":3\n" +
//                "  },\n" +
//                "  \"trigger_condition\":{\n" +
//                "    \"event_id\":\"share\",\n" +
//                "    \"event_property_name\":\"url\",\n" +
//                "    \"event_property_value\":\"/page/002\"\n" +
//                "  }\n" +
//                "\n" +
//                "}";
//        insertRule(new int[]{2, 3, 4, 5, 6, 7}, 3, 2, paramJson3);


        String paramJson4 = "{\n" +
                "  \"rule_id\":4,\n" +
                "  \"rule_model_id\":3,\n" +
                "  \"static_profile_conditions\":[\n" +
                "    {\"tag_name\":\"TAG0101\",\"compare\":\">\",\"tag_value\":[200]},\n" +
                "    {\"tag_name\":\"TAG0102\",\"compare\":\"bt\",\"tag_value\":[20,30]},\n" +
                "    {\"tag_name\":\"TAG0103\",\"compare\":\"=\",\"tag_value\":[\"male\"]}\n" +
                "  ],\n" +
                "  \"dynamic_profile_conditions\":{\n" +
                "    \"condition_id\":\"condition_01\",\n" +
                "    \"event_id\":\"ad_show\",\n" +
                "    \"event_property_name\":\"ad_id\",\n" +
                "    \"event_property_value\":\"ad001\",\n" +
                "    \"window_start\":\"2023-04-01\",\n" +
                "    \"window_end\":\"2023-04-30\",\n" +
                "    \"minCount\":5\n" +
                "  },\n" +
                "  \"trigger_condition\":{\n" +
                "    \"event_id\":\"add_cart\",\n" +
                "    \"event_property_name\":\"item_id\",\n" +
                "    \"event_property_value\":\"item001\"\n" +
                "  }\n" +
                "\n" +
                "}";


        int[] ids = new int[20000];
        for(int i=0;i<20000;i++){
            ids[i] = i;
        }
        insertRule2(ids,4,"condition_01",3,paramJson4);


    }


    public static void insertRule(int[] userIds, int ruleId, int ruleModelId, String ruleParamJson) throws SQLException, IOException {

        /**
         * 1. 预圈选人群
         */
        // 根据营销人员定义的规则参数里面的静态画像条件
        // 去elastic-search 中 预圈选 满足静态画像条件的人群
        // 查到了满足该规则的人群为： 1,3,5,7用户

        // 并将这些人的id，装入一个bitmap对象中
        RoaringBitmap bitmap = RoaringBitmap.bitmapOf();
        bitmap.add(userIds);

        // 将装好人群id的bitmap序列化成一个字节数组
        ByteArrayOutputStream baOut = new ByteArrayOutputStream();
        DataOutputStream dOut = new DataOutputStream(baOut);
        bitmap.serialize(dOut);

        byte[] bitmapBytes = baOut.toByteArray();


        /**
         * 2. 获取本规则所属的规则模型的运算机代码
         */
        Connection conn = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit37", "root", "root");

        // 首先，根据用户所选择的模型，去模型表中找到该模型的运算机代码
        PreparedStatement pt1 = conn.prepareStatement("select rule_model_calculator_groovy_code from rule_model_info where rule_model_id = ?");
        pt1.setInt(1, ruleModelId);
        ResultSet resultSet = pt1.executeQuery();
        resultSet.next();
        String rule_model_calculator_groovy_code = resultSet.getString("rule_model_calculator_groovy_code");


        /**
         * 将上面获取到的本规则的所有信息，插入 规则元信息表
         * 规则id
         * 规则模型id
         * 规则参数
         * 规则模型的运算机代码
         * 规则的预圈选人群
         */
        PreparedStatement pst = conn.prepareStatement("insert into rule_meta_info values(?,?,?,?,?)");
        //设置本规则的规则id为： 1
        pst.setLong(1, ruleId);
        pst.setInt(2, ruleModelId);
        pst.setString(3, rule_model_calculator_groovy_code);
        // // 设置本规则的预圈选人群bitmap
        pst.setBytes(4, bitmapBytes);
        pst.setString(5, ruleParamJson);
        pst.execute();

        // 关闭连接资源 ,
        pst.close();
        conn.close();


    }


    public static void insertRule2(int[] userIds, int ruleId, String condition_id, int ruleModelId, String ruleParamJson) throws SQLException, IOException {

        /**
         * 1. 预圈选人群
         */
        // 根据营销人员定义的规则参数里面的静态画像条件
        // 去elastic-search 中 预圈选 满足静态画像条件的人群
        // 查到了满足该规则的人群为： 1,3,5,7用户

        // 并将这些人的id，装入一个bitmap对象中
        RoaringBitmap bitmap = RoaringBitmap.bitmapOf();
        bitmap.add(userIds);

        // 将装好人群id的bitmap序列化成一个字节数组
        ByteArrayOutputStream baOut = new ByteArrayOutputStream();
        DataOutputStream dOut = new DataOutputStream(baOut);
        bitmap.serialize(dOut);

        byte[] bitmapBytes = baOut.toByteArray();


        /**
         * 2. 获取本规则所属的规则模型的运算机代码
         */
        Connection conn = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit37", "root", "root");

        // 首先，根据用户所选择的模型，去模型表中找到该模型的运算机代码
        PreparedStatement pt1 = conn.prepareStatement("select rule_model_calculator_groovy_code from rule_model_info where rule_model_id = ?");
        pt1.setInt(1, ruleModelId);
        ResultSet resultSet = pt1.executeQuery();
        resultSet.next();
        String rule_model_calculator_groovy_code = resultSet.getString("rule_model_calculator_groovy_code");


        /**
         * 3.发布跨越上线前后时间范围的画像条件的各用户的初始值，到redis
         */
        Jedis jedis = new Jedis("doitedu", 6379);
        for (int userId : userIds) {
            jedis.hset(ruleId + ":" + condition_id, userId + "", RandomUtils.nextInt(0,6) + "");
        }


        /**
         * 4. 将上面获取到的本规则的所有信息，插入 规则元信息表
         * 规则id
         * 规则模型id
         * 规则参数
         * 规则模型的运算机代码
         * 规则的预圈选人群
         */
        PreparedStatement pst = conn.prepareStatement("insert into rule_meta_info values(?,?,?,?,?,?,?)");
        //设置本规则的规则id为： 1
        pst.setLong(1, ruleId);
        pst.setInt(2, ruleModelId);
        pst.setString(3, rule_model_calculator_groovy_code);
        // // 设置本规则的预圈选人群bitmap
        pst.setBytes(4, bitmapBytes);
        pst.setString(5, ruleParamJson);
        pst.setInt(6, 1);
        pst.setString(7, "2023-04-15 17:42:40");
        pst.execute();


        // 关闭连接资源 ,
        pst.close();
        conn.close();


    }


}
